import java.awt.event.*;
import javax.swing.*;

public class KeyboardConnection
{
    Fenster f;

    public KeyboardConnection (Fenster f) {
        
        this.f = f;

        Action action = new AbstractAction() {
                public void actionPerformed(ActionEvent e) {
                    System.out.println("Hallo!!");
                }
            };            
        Action action1 = new AbstractAction() {
                public void actionPerformed(ActionEvent e) {
                    System.out.println("a");
                }
            };            
        Action action2 = new AbstractAction() {
                public void actionPerformed(ActionEvent e) {
                    System.out.println("s");
                }
            };            
        Action action3 = new AbstractAction() {
                public void actionPerformed(ActionEvent e) {
                    System.out.println("d");
                }
            };            
        Action action4 = new AbstractAction() {
                public void actionPerformed(ActionEvent e) {
                    System.out.println("w");
                }
            };            

        //keyStroke ermitteln, Version 1
        String keyStrokeAndKey = "control Z";
        KeyStroke keyStroke = KeyStroke.getKeyStroke(keyStrokeAndKey);

        String keyStrokeAndKey1 = "A";
        KeyStroke keyStroke1 = KeyStroke.getKeyStroke(keyStrokeAndKey1);
        String keyStrokeAndKey2 = "S";
        KeyStroke keyStroke2 = KeyStroke.getKeyStroke(keyStrokeAndKey2);
        String keyStrokeAndKey3 = "D";
        KeyStroke keyStroke3 = KeyStroke.getKeyStroke(keyStrokeAndKey3);
        String keyStrokeAndKey4 = "W";
        KeyStroke keyStroke4 = KeyStroke.getKeyStroke(keyStrokeAndKey4);

        //keyStroke ermitteln, Version 2
        //keyStroke = KeyStroke.getKeyStroke("ENTER");

        //KeyStroke einem JComponent hinzufuegen
        //eingabefeld.getInputMap().put(keyStroke, keyStrokeAndKey);
        //eingabefeld.getActionMap().put(keyStrokeAndKey, action);

        //KeyStroke allen Komponenten hinzufuegen
        f.getRootPane().getInputMap(JComponent.WHEN_ANCESTOR_OF_FOCUSED_COMPONENT).put(keyStroke, keyStrokeAndKey);
        f.getRootPane().getActionMap().put(keyStrokeAndKey, action);

        f.getRootPane().getInputMap(JComponent.WHEN_ANCESTOR_OF_FOCUSED_COMPONENT).put(keyStroke1, keyStrokeAndKey1);
        f.getRootPane().getActionMap().put(keyStrokeAndKey1, action1);
        f.getRootPane().getInputMap(JComponent.WHEN_ANCESTOR_OF_FOCUSED_COMPONENT).put(keyStroke2, keyStrokeAndKey2);
        f.getRootPane().getActionMap().put(keyStrokeAndKey2, action2);
        f.getRootPane().getInputMap(JComponent.WHEN_ANCESTOR_OF_FOCUSED_COMPONENT).put(keyStroke3, keyStrokeAndKey3);
        f.getRootPane().getActionMap().put(keyStrokeAndKey3, action3);
        f.getRootPane().getInputMap(JComponent.WHEN_ANCESTOR_OF_FOCUSED_COMPONENT).put(keyStroke4, keyStrokeAndKey4);
        f.getRootPane().getActionMap().put(keyStrokeAndKey4, action4);

    }
}
